package www.meng.com.controller;

import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.meng.com.config.DynamicDataSourceContextHolder;
import www.meng.com.entity.PeopleEntity;
import www.meng.com.service.BaseService;
import www.meng.com.service.PeopleService;
import www.meng.com.serviceImpl.BaseServiceImpl;
import www.meng.com.util.result.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Mr.Yan
 * @program : www.meng.com.controller
 * @create : 2020/10/20
 * @description : 人员 controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/people")
public class PeopleController {

    @Autowired
    private PeopleService peopleService;

    @RequestMapping(value = "/all")
    public R all() {
        DynamicDataSourceContextHolder.setDateSoureType("cs2");
        List<PeopleEntity> all = peopleService.findAll();
        return R.success.data(all);
    }

    @RequestMapping(value = "/custom")
    public R custom() {
        List<PeopleEntity> custom = peopleService.findCustom();
        return R.success.data(custom);
    }

    @Autowired
    private BaseService<PeopleEntity> baseService;

    @RequestMapping(value = "/main")
    public void main() {
//        log.info("find_All:" + baseService.find_All().toString());
//        log.info("find_All_Size:" + baseService.find_All_Size());
//        log.info("find_ObjByPK:" + baseService.find_ObjByPK(1).toString());
//        ArrayList<Long> find_ListByPKs = new ArrayList<>();
//        find_ListByPKs.add(1L);
//        find_ListByPKs.add(2L);
//        log.info("find_ListByPKs:" + baseService.find_ListByPKs(find_ListByPKs).toString());
//        PeopleEntity peopleEntity = new PeopleEntity();
//        peopleEntity.setpName("张三3");
//        try {
//            String s = baseService.find_ObjByWhere(peopleEntity).toString();
//            log.info("find_ObjByWhere:" + s);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        log.info("find_ListByWhere:" + baseService.find_ListByWhere(peopleEntity).toString());
//        log.info("find_ListByWhere_Size:" + baseService.find_ListByWhere_Size(peopleEntity));
//        peopleEntity.setpName("刘某人");
//        baseService.insert(peopleEntity);
//        log.info("insert PK:" + peopleEntity.getpId());
//        PeopleEntity insert_Selective = new PeopleEntity();
//        insert_Selective.setpName("刘某某");
//        baseService.insert_Selective(insert_Selective);
//        log.info("insert_Selective PK:" + insert_Selective.getpId());
//        List<PeopleEntity> insert_Batch = new ArrayList<>();
//        for (int i = 0; i <3 ; i++) {
//            PeopleEntity batchEntity = new PeopleEntity();
//            batchEntity.setpName("马克"+ i);
//            insert_Batch.add(batchEntity);
//        }
//        baseService.insert_Batch(insert_Batch);
//        for (PeopleEntity insert_batch : insert_Batch) {
//            log.info("insert_Batch PK:" + insert_batch.getpId());
//        }
//        PeopleEntity update_ByPK = new PeopleEntity();
//        update_ByPK.setpId(21L);
//        update_ByPK.setpName("马克up");
//        log.info("update_ByPK:" + baseService.update_ByPK(update_ByPK));
//
//        PeopleEntity update_Selective_ByPK = new PeopleEntity();
//        update_Selective_ByPK.setpId(22L);
//        update_Selective_ByPK.setpName("马克up");
//        log.info("update_Selective_ByPK:" + baseService.update_Selective_ByPK(update_Selective_ByPK));
//        List<PeopleEntity> update_Batch_ByPK = new ArrayList<>();
//        for (int i = 1; i <4 ; i++) {
//            PeopleEntity p = new PeopleEntity();
//            p.setpId(Long.parseLong(String.valueOf(i)));
//            p.setpName("updateBatch" + i);
//            update_Batch_ByPK.add(p);
//        }
//        log.info("update_Batch_ByPK:" + baseService.update_Batch_ByPK(update_Batch_ByPK));
//        List<PeopleEntity> update_BatchSelective_ByPK = new ArrayList<>();
//        for (int i = 6; i <9 ; i++) {
//            PeopleEntity p = new PeopleEntity();
//            p.setpId(Long.parseLong(String.valueOf(i)));
//            p.setpName("update_BatchSelective_ByPK" + i);
//            if(i==8){
//                p.setpAge("999");
//            }
//            update_BatchSelective_ByPK.add(p);
//        }
//        log.info("update_BatchSelective_ByPK:" + baseService.update_BatchSelective_ByPK(update_BatchSelective_ByPK));
//        log.info("delete_ByPK:" + baseService.delete_ByPK(64L));
//        Long [] pkarr = {68L,69L};
//        log.info("delete_ByPKArray:" + baseService.delete_ByPKArray(pkarr));

//        PeopleEntity upsert = new PeopleEntity();
//        upsert.setpId(78L);
//        upsert.setpName("110");
//        boolean upsert1 = baseService.upsert(upsert);
//        if(upsert1){
//            log.info("upsert：插入成功" + upsert.getpId());
//        } else {
//            log.info("upsert：更新成功" + upsert.getpId());
//        }
//
//        PeopleEntity upsert_SelectiveEntity = new PeopleEntity();
//        upsert_SelectiveEntity.setpId(999L);
//        upsert_SelectiveEntity.setpAge("21");
//        boolean upsert_Selective = baseService.upsert_Selective(upsert_SelectiveEntity);
//        if(upsert_Selective){
//        log.info("upsert_Selective：插入成功"+ upsert.getpId());
//        } else {
//            log.info("upsert_Selective：更新成功"  + upsert.getpId());
//        }
        PeopleEntity pageEntity = new PeopleEntity();
        pageEntity.setpAge("18");
        PageResult page = baseService.find_Page(1, 10, pageEntity, "age");

        log.info("totalRow:" + page.getTotalRow());
        log.info("totalPage:" + page.getTotalPage());
        log.info("list:" + page.getList());
    }
}
